﻿using ElsCircuit.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ElsCircuit
{
    /// <summary>
    /// circuit.xaml 的交互逻辑
    /// </summary>
    public partial class circuit : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public EnumCircuitType CircuitType { get; set; }

        /// <summary>
        /// 组
        /// </summary>
        public int group { get; set; } = 0;
       public  circuitModel mode { get; set; }
        public bool mouseFlag { get; set; }
        public circuit()
        {
            InitializeComponent();
            mode = this.DataContext as circuitModel;
            this.Loaded += Circuit_Loaded;

            this.AllowDrop = true;

            this.can.AllowDrop = true;
            this.can.MouseDown += Zhuangbeilan_MouseDown;
            this.can.MouseMove += Zhuangbeilan_MouseMove;
            this.can.MouseUp += Zhuangbeilan_MouseUp;
            this.can.Drop += Can_Drop;
            // this.can.MouseDown += ZhuangBeiList_MouseDown;
        }
        private void Can_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(circuitModel)))
            {
                var data = e.Data.GetData(typeof(circuitModel)) as circuitModel;
                if (data.group==group||data.group<group)
                {
                    return;
                }
                mode.pathData = data.pathData;
                mode.CircuitType=data.CircuitType;
                mode.colorType = data.colorType;
                CircuitType = mode.CircuitType;
                mode.Draw();
                mode.DataChangeEx();
            }
        }
        //private void ZhuangBeiList_MouseDown(object sender, MouseButtonEventArgs e)
        //{
        //    if (e.ChangedButton == MouseButton.Right && e.ButtonState == MouseButtonState.Pressed)
        //    {
        //        RightClick?.Invoke(this.DataContext);
        //    }
        //}

        private void Zhuangbeilan_MouseUp(object sender, MouseButtonEventArgs e)
        {

            mouseFlag = false;
        }

        private void Zhuangbeilan_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                if (mouseFlag)
                {
                    DragDrop.DoDragDrop(this,
                                 this.DataContext,
                                 DragDropEffects.Copy);
                }

        }

        private void Zhuangbeilan_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                mouseFlag = true;
            }
        }

        private void Circuit_Loaded(object sender, RoutedEventArgs e)
        {
            mode.group= group;
            if (CircuitType != EnumCircuitType.non)
            {
                mode.CircuitType = CircuitType;
            }
            mode.Draw();
        }
    }
}
